package com.zhuss.order.processor;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.zhuss.WxCertParam;
import com.zhuss.bean.bo.order.order.WxRefundBo;
import com.zhuss.bean.vo.order.order.WxRefundVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.enums.IsEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;

/**
 * 微信退款
 *  processor
 * @Author: zhushanshuo
 * @Date: 2020/11/03 19:21
 */
@Component("wxRefundProcessor")
public class WxRefundProcessor extends AbstractProcessor<WxRefundVo, WxRefundBo> implements Processor<WxRefundVo, WxRefundBo> {

	@Value("${wxPayCert.path}")
    private String wxPayCertPath;
	
	@Value("${wxpay.appId}")
    private String appId;
	
	@Value("${wxpay.mchId}")
    private String mchId;
	
	@Value("${wxpay.key}")
    private String key;
	
	@Value("${wxpay.tradeType}")
	private String tradeType;
	
	@Value("${wxpay.notifyUrl}")
	private String notifyUrl;
	
    
    /**
     * 校验
     */
    @Override
    public void check(WxRefundBo bo) throws DzcxException {
    }
    
    /**
     * 操作方法
     * @throws Exception 
     */
    @Override
    public WxRefundVo process(WxRefundBo param) throws DzcxException {
    	
    	WxRefundVo result = new WxRefundVo();
    	
    	param.buildAppId(appId).buildMchId(mchId).buildKey(key);
    	
        WxCertParam wxCertParam = new WxCertParam(param.getAppId(), param.getMchId(), param.getKey(), IsEnum.是.key(), wxPayCertPath);
        WXPay wxPay = new WXPay(wxCertParam);

        //根据微信支付的api来设置
        Map<String,String> data = new HashMap<>();
        data.put("appid",param.getAppId());
        data.put("mch_id",param.getMchId());         //商户号
        data.put("out_trade_no", param.getOrderNo());   //交易号
        data.put("nonce_str", param.getNonceStr());   // 随机字符串小于32位
        data.put("out_refund_no", param.getOrderRefundNo());
        data.put("total_fee", param.getTotalFee() + "");
        data.put("refund_fee", param.getApplyRefundFee() + "");
        String sign = null;
        
		try {
			sign = WXPayUtil.generateSignature(data, wxCertParam.getKey());
		} catch (Exception e) {
			e.printStackTrace();
			throw new DzcxException(ExceptionCode.微信支付获取签名错误);
		}  
		
		//签名
        data.put("sign",sign);

        /** wxPay.refund 这个方法中调用微信申请退款接口 */
        Map<String, String> respData = null;
		try {
			respData = wxPay.refund(data);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DzcxException(ExceptionCode.微信订单退款错误);
		}
        if (respData.get("return_code").equals("SUCCESS")){
        	
        	if (respData.get("result_code").equals("FAIL")) {
        		throw new DzcxException(ExceptionCode.微信退款失败, String.valueOf(respData.get("err_code_des")));
        	}

            //退款返回的参数
            BeanUtils.copyProperties(param, result);
            result.setTotalFee(Long.valueOf(respData.get("total_fee")));
            result.setRefundFee(Long.valueOf(respData.get("refund_fee")));
            result.setTransactionId(respData.get("transaction_id"));
            result.setWxRefundId(respData.get("refund_id"));
            //result.setSettlementRefundFee(Long.valueOf(respData.get("settlement_refund_fee")));
            //result.setSettlementTotalFee(Long.valueOf(respData.get("settlement_total_fee")));
            result.setCashRefundFee(Long.valueOf(respData.get("cash_refund_fee")));
            result.setNonceStr(respData.get("nonce_str"));
            result.setReturnMsg(respData.get("return_msg"));
            result.setResultCode(respData.get("result_code"));
            result.setReturnCode(respData.get("return_code"));
            return result;
        }
        return result;
    }
    
    /**
     * 操作日志
     */
	@Override
	public WxRefundVo log(WxRefundBo param) throws DzcxException {
    	return null;
	}
}
